#include "graphsupport/solve/facilitylocation/IFacilityLocationSolver.h"

#include <iostream>
#include <assert.h>

IFacilityLocationSolver::~IFacilityLocationSolver()
{
}

FacilitiesGraph::FacilityPriceType IFacilityLocationSolver::computeCost(
	FacilitiesGraph & fgraph,
	const std::vector< char > & isFacilityOpenVec,
	const std::vector< int > & cityWitnessVec
)
{
	int citiesCount = fgraph.getCitiesCount();
	int facilitiesCount = fgraph.getFacilitiesCount();

	FacilitiesGraph::FacilityPriceType result = 0;

	for ( int city = 0; city < citiesCount; city++ )
	{
		int witness = cityWitnessVec[city];
		if ( witness == -1 )
		{
			return fgraph.getInfEdge();
		}
		assert( witness > -1 );
		assert( witness < facilitiesCount );
		if ( !isFacilityOpenVec[witness] )
		{
			return fgraph.getInfEdge();
		}
		result += fgraph.getFSEdge( city, witness );
	}
	for ( int facility = 0; facility < facilitiesCount; facility++ )
	{
		if ( isFacilityOpenVec[facility] )
		{
			result += fgraph.facilitiesPrices[facility];
		}
	}
	return result;
}
